<template>
  <div class='container-Subjects'>
    <!-- 卡片 -->
    <el-card>
      <!-- 筛选按钮 -->
      <el-row>
        <el-col :span="18">
        <el-form inline label-width="80px" size="small">
        <el-form-item label="学科名称">
          <el-input v-model="reqParams.subjectName" placeholder="输入关键字"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button @click="clear()">清除</el-button>
          <el-button type="primary" @click="search()">搜索</el-button>
        </el-form-item>
        </el-form>
        </el-col>
        <!-- 新增学科按钮 -->
        <el-col :span="6">
          <el-button style="float:right" icon="el-icon-edit" type="success" size="small" @click="updateBtn">新增学科</el-button>
        </el-col>
      </el-row>
      <!-- 总计 -->
      <el-alert show-icon :title= "`数据一共${total}条`" type="info" :closable="false" style="margin-bottom:15px"></el-alert>
      <!-- 表格 -->
      <el-table :data="subjects" >
       <el-table-column label="序号" type="index"></el-table-column>
       <el-table-column label="学科名称" prop="subjectName"></el-table-column>
       <el-table-column label="创建者" prop="username"></el-table-column>
       <el-table-column label="创建日期" width="160px">
        <!-- 作用域插槽 -->
        <template slot-scope="scope">
          {{ scope.row.addDate | parseTimeByString }}
        </template>
       </el-table-column>
       <el-table-column label="前台是否显示" align="center">
        <template slot-scope="scope">
          {{ scope.row.isFrontDisplay === 1 ? '是' :'否' }}
        </template>
       </el-table-column>
       <el-table-column label="二级目录" prop="twoLevelDirectory" align="center"></el-table-column>
       <el-table-column label="标签" prop="tags" align="center"></el-table-column>
       <el-table-column label="题目数量" prop="totals" align="center"></el-table-column>
       <el-table-column label="操作" width="230px" >
        <template slot-scope="scope">
          <el-link @click="toDiretory(scope.row)" :underline="false" type="primary">学科分类</el-link>
          <el-link @click="toTag(scope.row)" :underline="false" type="primary">学科标签</el-link>
          <el-link @click="updateBtn(scope.row)"  :underline="false" type="primary">修改</el-link>
          <el-link @click="delSubject(scope.row.id)" :underline="false" type="primary">删除</el-link>
        </template>
       </el-table-column>
      </el-table>
      <!-- 分页 -->
      <!-- current-change事件  当前页码改变时会触发 -->
      <!-- page-sizes 每页显示个数选择器的选项设置 -->
      <!-- size-change 每页显示条数改变时触发 -->
      <el-pagination
      class="pager"
      background
      layout="prev, pager, next, sizes, jumper"
      :total="total"
      :page-size="reqParams.pagesize"
      :current-page="reqParams.page"
      @current-change= "changePager"
      :page-sizes=[5,10,20,50]
      @size-change= "changeSize"
      > </el-pagination>

    </el-card>
    <!-- 点击弹窗组件 -->
    <subjectsAdd
      :showDialog.sync="showDialog" ref="updateAdd"
      ></subjectsAdd>
    </div>
</template>

<script>
// 导入学科列表接口 删除接口
import { list as subjectsList, remove as subjectDel } from '@/api/hmmm/subjects'
// 引入学科组件
import subjectsAdd from '@/module-hmmm/components/subjects-add'
export default {
  components: { subjectsAdd },
  data () {
    return {
      // 筛选参数
      reqParams: {
        page: 1, // 页码
        pagesize: 10, // 每页条数
        subjectName: null // 学科名
      },
      // 学科列表
      subjects: [],
      // 总条数
      total: 0,
      // 默认不显示弹层
      showDialog: false
    }
  },
  // 组件初始化 默认显示列表
  created () {
    this.getSubjects()
  },
  methods: {
    // 获取学科列表
    async getSubjects () {
      const { data } = await subjectsList(this.reqParams)
      // 给data赋值
      this.subjects = data.items
      this.total = data.counts
    },
    // 分页
    changePager (newPager) {
      this.reqParams.page = newPager
      this.getSubjects()
    },
    // 点击改变条数后渲染页面
    changeSize (newSize) {
      this.reqParams.page = 1 // 从第一页开始
      this.reqParams.pagesize = newSize
      this.getSubjects()
    },
    // 清除
    clear () {
      this.reqParams.subjectName = null
      this.reqParams.page = 1
      this.getSubjects()
    },
    // 搜索
    search () {
      this.reqParams.page = 1
      this.getSubjects()
    },
    // 去目录 形参row相当于当前点击的items
    toDiretory (row) {
      const { id, subjectName: name } = row
      console.log(row)
      this.$router.push({ path: 'directorys', query: { id, name } }) // id name 是约定好的传参
    },
    // 去标签
    toTag (row) {
      const { id, subjectName: name } = row
      this.$router.push({ path: 'tags', query: { id, name } })
    },
    // 删除
    delSubject (id) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(
        async () => {
          await subjectDel({ id })
          this.$message.success('删除成功')
          this.getSubjects()
        }
      ).catch(e => {})
    },
    // 点击新增 修改
    async updateBtn (row) {
      await this.$refs.updateAdd.updateDirectory(row)
      this.showDialog = true
    }
  }
}

</script>

<style scoped lang='scss'>
.container-Subjects{
  padding:10px 15px;
  .pager{
    margin-top: 15px;
    margin-bottom: 15px;
    float:right
  }
  .el-link {
  margin-right: 10px;
  }
}
</style>
